// pages/electronic/write_off.js
import {
  OrdersModel
} from "../../utils/models/orders/Orders-models";
var ordersModel = new OrdersModel;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isoff: false,

    inputLenth: 4,
    dev: '',
    scanValue: '',
    inputWidth: '104rpx',
    inputHeight: '104rpx',
    business: {},
    order: {},
    num: 1,
    usable: 3,
    oid:0,
  },

  // 点击自助核销
  goOff: function() {
    var that = this;
    var param = {"dev":this.data.dev,"oid":this.data.oid};
    ordersModel.writeOff(param,function(res){
      if(res.code > 0){
        wx.showToast({
          icon : 'none',
          title: '核销成功',
          success: function() {
            that.setData({
              dev: '',
              isoff: false
            })
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/my/my',
              })
            }, 1500);
          }
        })
      }else{
        wx.showToast({
          icon : 'none',
          title: '核销密码不正确（核销后不可撤销哦，请交由店员操作）',
          success: function() {
            that.setData({
              dev: '',
              isoff: true
            })
          }
        })
      }
    })
    
  },

  setupPasswordComplete(e) {
    if(e.detail.length == this.data.inputLenth) {
      var isoff = true;
    }else{
      var isoff = false;
    }
    this.setData({
      'dev': e.detail,
      'isoff': isoff
    })
  },

  // 数量增加
  jia: function() {
    var num = parseInt(this.data.num) + 1 ;
    if(num<=this.data.usable) {
      this.setData({
        num : num,
      })
    }
  },

  // 数量减少
  jian: function(){
    var num = parseInt(this.data.num) - 1 ;
    if(num >= 1 && num<=this.data.usable) {
      this.setData({
        num : num,
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var param = {
      oid:options.id
    };
    ordersModel.voucher(param, function (res) {
      that.setData({
        business: res.data.business,
        order: res.data.order,
        oid:options.id
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.passwordBox = this.selectComponent('#passwordBox') // 获取密码框组件，用来操作组件内部的方法
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})